// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright © 2019 Ariadne Devos
// sHT -- introduce sHT_index_iterate

// sHT_index_iterate does index speculative index clipping itself
// (see Spectre), reduces work for formal analysis (is the correct
// comparison operator used? Is the index an integer? Is it clipped?),
// centralises optimisation decisions (branch predicition information),
// and is informative.

@ abstract_index_iterate0 @
identifier i;
expression n;
iterator name sHT_index_iterate;
expression a;
type T;
@@
(
- for (i = 0; \(i < n\|sHT_gt(n, i)\|!sHT_ge(i, n)\); i++)
+ sHT_index_iterate(i, n)
  {
(
    ... sHT_index_nospec(i, n) ...
|
    ... a[i] ...
)
  }
|
+ size_t i;
- for (T i = 0; \(i < n\|sHT_gt(n, i)\|!sHT_ge(i, n)\); i++)
+ sHT_index_iterate(i, n)
  {
(
    ... sHT_index_nospec(i, n) ...
|
    ... a[i] ...
)
  }
)

@ no_clip_assign @
identifier i;
expression n;
@@
  sHT_index_iterate(i, n) {
  ...
- i = sHT_index_nospec(i, n);
  ...
  }

@ has_header @
@@
  #include <sHT/index.h>

@ use_index @
statement S;
@@
  sHT_index_iterate(...)
  S

@ add_header depends on use_index && !has_header @
@@
  #include <sHT/...>
+ #include <sHT/index.h>
